import axios from 'axios';
import query from 'query-string';

const BASE_URL = '/product/sku';


export interface SkuParam {
    spuId: string|number;
    name?: string;
    page?: number;
    size?: number;
    sort?: Array<string>;
}

export interface SkuResponse {
    id?: number;
    spuId: number;
    skuName: string;
    skuImg?: string;
    salePrice?: string;
    promotionPrice?: string;
    skuCode?: string;

    createdBy?: string;
    updatedBy?: string;
    createdAt?: string;
    updatedAt?: string;
}

export interface SkuSaveCommand {
    id?: number;
    spuId: number;
    skuName: string;
    skuImg?: string;
    salePrice?: string;
    promotionPrice?: string;
    skuCode?: string;
}

export interface SkuResponseList {
    list: SkuResponse[];
    total: number;
}


// 通过SkuParam参数获取分页列表
export function pageSku(params: SkuParam) {
    return axios.get<SkuResponseList>(`${BASE_URL}/page`, {
        params, paramsSerializer: (obj) => {
            return query.stringify(obj);
        }
    })
}

// 通过id获取sku信息
export function getSku(id: number) {
    return axios.get<SkuResponse>(`${BASE_URL}/${id}`);
    
}

export function listSku(params: SkuParam) {
    return axios.get<SkuResponse[]>(`${BASE_URL}/list`, {
        params, paramsSerializer: (obj) => {
            return query.stringify(obj);
        }
    });
}

// 添加sku
export function addSku(params: SkuSaveCommand) {
    return axios.post(`${BASE_URL}`, params);
}

// 更新sku
export function updateSku(params: SkuSaveCommand) {
    return axios.put(`${BASE_URL}`, params);
}

// 批量删除sku
export function deleteSku(ids: number | Array<number>) {
    return axios.delete(`${BASE_URL}/${ids}`);
}